from typing import Optional

from keras.src.backend.openvino.trainer import OpenVINOTrainer
from keras.src.trainers.data_adapters.array_slicing import can_slice_array


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def addTwoNumbers(self,l1: Optional[ListNode],l2: Optional[ListNode]) -> Optional[ListNode]:
        cur = dummy = ListNode()  # 初始化哑节点和当前指针
        carry = 0

        while l1 or l2 or carry:
            # 计算当前位的总和
            val1 = l1.val if l1 else 0
            val2 = l2.val if l2 else 0
            total = val1 + val2 + carry

            # 创建新节点并链接到链表
            cur.next = ListNode(total % 10)  # 关键修正：通过 cur.next 链接
            cur = cur.next  # 移动 cur 到新节点

            # 更新进位
            carry = total // 10

            # 移动 l1 和 l2 指针
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next

        return dummy.next  # 返回哑节点的下一个节点（实际头节点）
class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:[Optional[ListNode]],l2:[Optional[ListNode]]):
        cur=dummy=ListNode(0)
        carry=0
        while l1 or l2 or carry:
            val1=l1.val if l1 else 0
            val2=l2.val if l2 else 0
            total=val1+val2+carry
            #创建新节点并链接到链表
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10

            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode(0)
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next

class LisNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+ carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next
class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2 else 0)+carry
            cur.next=ListNode(total%10)
            carry=total//10
            cur=cur.next
            if l1:
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next
class ListNode:
    def __init__(self,val=0 ,next=next):
        self.val=val
        self.next=next
class Solution:
    def addTwoNumbers(self,l1:Optional[ListNode],l2:Optional[ListNode]):
        cur=dummy=ListNode()
        carry=0
        while l1 or l2 or carry:
            total=(l1.val if l1 else 0)+(l2.val if l2  else 0)+carry
            cur.next=ListNode(total%10)
            cur=cur.next
            carry=total//10
            if l1 :
                l1=l1.next
            if l2:
                l2=l2.next
        return dummy.next



# 创建 l1 = 2 → 4 → 3
l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)

# 创建 l2 = 5 → 6 → 4
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

# 调用函数并打印结果
solution = Solution()
result = solution.addTwoNumbers(l1, l2)
result1=solution.addTwoNumbers(l1,l2)
# 遍历打印结果链表
while result:
    print(result.val, end=" → " if result.next else " Finish ")
    result = result.next
# 输出：7 → 0 → 8
while result1:
    print(result1.val,end="->"if result1.next else "")
    result1=result1.next



























